
"""
    :copyright: © 2019 by the Lin team.
    :license: MIT, see LICENSE for more details.
"""

from lin.redprint import Redprint
from lin import route_meta, group_required, login_required
from lin.exception import Success
from flask import jsonify
from .user import my_client
from .posion import my_posion
from .forms import ClientSearchForm, CreateOrUpdateClientForm, BankCardSearchForm, CreateOrUpdateBankCardForm

user_api = Redprint("user")


@user_api.route("/<usn>", methods=["GET"])
@login_required
def get_user(usn):
    client = my_client.get_user(usn)
    return jsonify(client)


@user_api.route("/page=<page>", methods=["GET"])
@login_required
def get_all_user(page):
    client = my_client.get_all_users(page=page)
    return jsonify(client)


@user_api.route("/search", methods=["GET"])
@login_required
def get_search_by_name():
    form = ClientSearchForm().validate_for_api()
    client = my_client.get_user_by_name(form.q.data)
    return jsonify(client)


@user_api.route("/searchcard", methods=["GET"])
@login_required
def get_user_by_user_card_no():
    form = ClientSearchForm().validate_for_api()
    client = my_client.get_user_by_card(form.q.data)
    return jsonify(client)


@user_api.route("/", methods=["POST"])
@login_required
def new_user():
    form = CreateOrUpdateClientForm().validate_for_api()
    my_client.new_user(form)
    return Success(msg="添加用户成功")


@user_api.route("/<usn>", methods=["POST"])
@login_required
def update_user(usn):
    form = CreateOrUpdateClientForm().validate_for_api()
    my_client.update_user(usn, form)
    return Success(msg="更新用户信息成功")


@user_api.route("/<usn>", methods=["DELETE"])
@login_required
def cancel_user(usn):
    my_client.cancell_user(usn)
    return Success(msg="删除用户成功")


@user_api.route("/rows", methods=["GET"])
@login_required
def get_user_rows():
    rows = my_client.get_client_count()
    return jsonify(rows)


@user_api.route("/card/", methods=["POST"])
@login_required
def new_bank_card(usn):
    form = CreateOrUpdateBankCardForm().validate_for_api()
    my_client.new_user_bank(usn, form)
    return Success("新增银行卡成功")


@user_api.route("/card/", methods=["POST"])
@login_required
def update_bank_info(usn):
    form = CreateOrUpdateBankCardForm().validate_for_api()
    my_client.update_user_bank(usn, form)
    return Success(msg="更新用户银行卡成功")


@user_api.route("/card/user=<usn>", methods=["GET"])
@login_required
def get_bank_card(usn):
    bank_card = my_client.get_bank_card(usn)
    return jsonify(bank_card)


@user_api.route("/card/bankcard=<cardNo>", methods=["GET"])
@login_required
def search_bank_card(cardNo):
    client = my_client.get_client_by_card(cardNo)
    return jsonify(client)


@user_api.route("/posion/usn=<usn>&page=<page>", methods=["GET"])
@login_required
def get_posion_by_user(usn, page):
    posion = my_posion.get_posion(usn, page)
    return jsonify(posion)


@user_api.route("/posion/user=<usn>", methods=["GET"])
@login_required
def get_posion_quntity_by_user(usn):
    quntity = my_posion.get_quantity(usn)
    return jsonify(quntity)


@user_api.route("/posion/user=<usn>", methods=["GET"])
@login_required
def get_posion_count(usn):
    count = my_posion.get_count(usn)
    return jsonify(count)